package com.chianghao.qyt.controller.manage_api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chianghao.qyt.controller.manage_api.dto.SysOperationRoleAddDto;
import com.chianghao.qyt.controller.manage_api.dto.SysOperationRoleModifyDto;
import com.chianghao.qyt.controller.manage_api.vo.RolePageVo;
import com.chianghao.qyt.controller.manage_api.vo.RoleSimpleVo;
import com.chianghao.qyt.entity.SysOperationRole;
import com.chianghao.qyt.entity.SysOperationRoleMenu;
import com.chianghao.qyt.service.SysOperationRoleService;
import com.chianghao.frame.core.model.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("manage/api/operation-role")
public class SysOperationRoleController {

    @Resource
    SysOperationRoleService sysOperationRoleService;


    @PostMapping("page")
    public Page<RolePageVo> page(
            @RequestParam("pageIndex") Integer pageIndex,
            @RequestParam("pageSize") Integer pageSize
    ){
        IPage<SysOperationRole> ipage = sysOperationRoleService.page(pageIndex,pageSize);
        Page<RolePageVo> page = new Page<>();
        page.setTotal(ipage.getTotal());
        Map<Long, Set<SysOperationRoleMenu>> roleToMenuIdsMap;
        if(!ipage.getRecords().isEmpty()){
            roleToMenuIdsMap = sysOperationRoleService.queryRoleToMenuIdsMap(ipage.getRecords().stream().map(SysOperationRole::getId).collect(Collectors.toSet()));
        } else {
            roleToMenuIdsMap = null;
        }
        page.setItems(ipage.getRecords().stream().map(i->{
            RolePageVo vo = new RolePageVo();
            vo.setId(i.getId());
            vo.setRoleName(i.getRoleName());
            vo.setRemark(i.getRemark());
            Set<String> menusIds = new HashSet<>();
            Set<String> menusHalfIds = new HashSet<>();
            if(roleToMenuIdsMap!=null){
                Set<SysOperationRoleMenu> sors = roleToMenuIdsMap.get(i.getId());
                if(sors != null){
                    for(SysOperationRoleMenu sor : sors){
                        if(sor.getState()==0){
                            menusHalfIds.add(sor.getMenuId());
                        }
                        if(sor.getState()==1){
                            menusIds.add(sor.getMenuId());
                        }
                    }
                }
            }
            vo.setMenuIds(menusIds);
            vo.setMenuHalfIds(menusHalfIds);
            return vo;
        }).toList());
        return page;
    }


    @GetMapping("simple/all")
    public List<RoleSimpleVo> allSimple(){
        List<SysOperationRole> roleList = this.sysOperationRoleService.all();
        return roleList.stream().map(i->{
            RoleSimpleVo vo = new RoleSimpleVo();
            vo.setId(i.getId());
            vo.setName(i.getRoleName());
            return vo;
        }).toList();
    }



    @PostMapping("add")
    @Operation(summary = "添加角色")
    public Boolean add(@RequestBody SysOperationRoleAddDto dto){
        sysOperationRoleService.add(dto.getName(),dto.getRemark(),dto.getMenuCheckedIds(),dto.getMenuHalfCheckedIds());
        return true;
    }

    @PostMapping("modify")
    @Operation(summary = "修改角色")
    public Boolean modify(@RequestBody SysOperationRoleModifyDto dto){
        sysOperationRoleService.modify(dto.getId(),dto.getName(),dto.getRemark(),dto.getMenuCheckedIds(),dto.getMenuHalfCheckedIds());
        return true;
    }

    @DeleteMapping("delete")
    @Operation(summary = "删除角色")
    public Boolean delete(@RequestParam("id") String id){
        sysOperationRoleService.delete(id);
        return true;
    }

}
